package com.qgs.secsgem.secs.controller;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qgs.secsgem.secs.eq.SECSCommand;
import com.qgs.secsgem.secs.eq.runnable.PID10Runnable;
import com.qgs.secsgem.secs.SecsException;
import com.qgs.secsgem.secs.SecsMessage;
import com.qgs.secsgem.secs.entity.MesRequestMessage;
import com.qgs.secsgem.secs.entity.common.*;
import com.qgs.secsgem.secs.secs2.Secs2Exception;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/secs")
public class SECSExampleController {
    private static final Logger logger = LoggerFactory.getLogger(SECSExampleController.class);



    //外部发起SECS/GEM 请求。
    //S1F3 OK
    @PostMapping(value = "S1F3")
    private void S1F3(@RequestBody MesRequestMessage msg) throws InterruptedException, SecsException {
        List<String> s = new ArrayList<>();

        JSONObject jsonObject = JSONObject.parseObject(msg.getBody());
        String vidType = jsonObject.getString("vidType");
        JSONArray o = jsonObject.getJSONArray("vidList");
        for(int i=0;i<o.size();i++){
            s.add(o.getString(i));
        }

        Optional<SecsMessage> resVIDNodes = SECSCommand.queryEQPStatus(s, vidType, PID10Runnable.comm);
    }

    @PostMapping(value = "S1F11")
    private void S1F11() throws InterruptedException, SecsException {
        List<String> s = new ArrayList<>();
        s.add("1001");
        s.add("1002");
        s.add("1003");
        SECSCommand.querySVNameList(s,PID10Runnable.VID_Type,PID10Runnable.comm);
    }

    @PostMapping(value = "S1F13")
    private void S1F13() throws InterruptedException, SecsException, Secs2Exception {
        SECSCommand.establishCommunications(PID10Runnable.comm);
    }

    @PostMapping(value = "S1F15")
    private void S1F15() throws InterruptedException, SecsException, Secs2Exception {
        System.out.println(SECSCommand.requestOffLine(PID10Runnable.comm));

    }

    @PostMapping(value = "S1F17")
    private void S1F17() throws InterruptedException, SecsException, Secs2Exception {
        System.out.println(SECSCommand.requestOnLine(PID10Runnable.comm));;
    }

    @PostMapping(value = "S2F13")
    private void S2F13() throws InterruptedException, SecsException, Secs2Exception {
        List<String> s = new ArrayList<>();
        s.add("1001");
        s.add("1002");
        s.add("1003");
//        PID10Runnable.querySVNameList(s);
        SECSCommand.eqConstantReq(s,PID10Runnable.VID_Type,PID10Runnable.comm);
    }

    @PostMapping(value = "S2F15")
    private void S2F15() throws InterruptedException, SecsException, Secs2Exception {
        List<ECNode> s = new ArrayList<>();
        s.add(new ECNode("1","155","I4"));
        s.add(new ECNode("2","155.5","F4"));
        s.add(new ECNode("3","155","A"));
//        PID10Runnable.querySVNameList(s);
        SECSCommand.newEquipmentConstantSend(s,PID10Runnable.ECID_Type,PID10Runnable.comm);
    }

    @PostMapping(value = "S2F17")
    private void S2F17() throws InterruptedException, SecsException, Secs2Exception {
        SECSCommand.dateAndTimeRequest(PID10Runnable.comm);
    }

    @PostMapping(value = "S2F23")
    private void S2F23() throws InterruptedException, SecsException, Secs2Exception {
        List<String> vidList = new ArrayList<>();
        vidList.add("1001");
        vidList.add("1002");
        vidList.add("1003");
        TraceNode traceNode = new TraceNode("100a","A","000002",100,"U4",1,"U1",PID10Runnable.VID_Type);
        traceNode.setVidList(vidList);
        SECSCommand.traceInitialSend(traceNode,PID10Runnable.comm);
    }
    @PostMapping(value = "S2F29")
    private void S2F29() throws InterruptedException, SecsException, Secs2Exception {
        List<String> ecids = new ArrayList<>();
        ecids.add("1001");
        ecids.add("1002");
        ecids.add("1003");

        SECSCommand.equipmentConstantNameListRequest(ecids, PID10Runnable.ECID_Type,PID10Runnable.comm);
    }


    private ReportNode createReport(String reportId,String reportIdType,List<ValueNode> valueNodes) {
        ReportNode reportNode = new ReportNode();
        reportNode.setReportId(reportId);
        reportNode.setReportIdType(reportIdType);
        reportNode.setValueList(valueNodes);
        return reportNode;
    }
    private ReportEventNode createReportEvent(String ceid,String ceidType,List<ValueNode> valueNodes) {
        ReportEventNode reportEventNode = new ReportEventNode();
        reportEventNode.setCeid(ceid);
        reportEventNode.setCeidType(ceidType);
        reportEventNode.setValueNodes(valueNodes);
        return reportEventNode;
    }

    @PostMapping(value = "S2F33_define_report")
    private void S2F33_define_report() throws InterruptedException, SecsException, Secs2Exception {
        List<ReportNode> reportNodes = new ArrayList<>();

        //report1
        //注意：vids要按顺序
        //1. 这个代表给 reportId=1001 定义以下 vids
        List<ValueNode> vids = new ArrayList<>();
        vids.add(new ValueNode("U4",1));
        vids.add(new ValueNode("U4",2));
        vids.add(new ValueNode("U4",3));
        //2. 这样代表 删除 reportId=1001  下的所有vids（因为vids 是空）
        reportNodes.add(createReport("1001",PID10Runnable.REPORTID_Type,vids));


        //report2
        List<ValueNode> vids2 = new ArrayList<>();
        vids2.add(new ValueNode("U4",1001));
        vids2.add(new ValueNode("U4",1002));
        vids2.add(new ValueNode("U4",1003));
        reportNodes.add(createReport("1002",PID10Runnable.REPORTID_Type,vids2));

        SECSCommand.defineReport(reportNodes,PID10Runnable.comm);
    }

    @PostMapping(value = "S2F33_del_allReportAndLink")
    private void S2F33_del_allReportAndLink() throws InterruptedException, SecsException, Secs2Exception {
        List<ReportNode> reportNodes = new ArrayList<>();
        SECSCommand.defineReport(reportNodes,PID10Runnable.comm);
    }


    //deletes report type RPTID. All CEID links to this RPTID are also deleted.
    @PostMapping(value = "S2F33_del_OneReportAndLinkedEvent")
    private void S2F33_del_OneReportAndLinkedEvent() throws InterruptedException, SecsException, Secs2Exception {
        List<ReportNode> reportNodes = new ArrayList<>();

        //report1
        //注意：vids要按顺序
        //1. 这个代表给 reportId=1001 定义以下 vids
        List<ValueNode> vids = new ArrayList<>();
        //2. 这样代表 删除 reportId=1001  下的所有vids（因为vids 是空）
        reportNodes.add(createReport("1001",PID10Runnable.REPORTID_Type,vids));

        SECSCommand.defineReport(reportNodes,PID10Runnable.comm);
    }


    @PostMapping(value = "S2F35_ReportAndEventLinked")
    private void S2F35_ReportAndEventLinked() throws InterruptedException, SecsException, Secs2Exception {
        List<ReportEventNode> reportEventNodes = new ArrayList<>();

        //report-event link 1
        //注意：reportIds要按顺序
        //1. 这个代表给 ceid=101 定义以下 reportIds
        List<ValueNode> rptids = new ArrayList<>();
        rptids.add(new ValueNode("U4",1));
        rptids.add(new ValueNode("U4",2));
        rptids.add(new ValueNode("U4",3));
        //2. 这样代表 删除 reportId=1001  下的所有vids（因为vids 是空）
        reportEventNodes.add(createReportEvent("101",PID10Runnable.CEID_Type,rptids));


        //report-event link 2
        List<ValueNode> rptids2 = new ArrayList<>();
        rptids2.add(new ValueNode("U4",1001));
        rptids2.add(new ValueNode("U4",1002));
        rptids2.add(new ValueNode("U4",1003));
        reportEventNodes.add(createReportEvent("101",PID10Runnable.CEID_Type,rptids2));

        SECSCommand.linkEventReport(reportEventNodes,PID10Runnable.comm);
    }

    @PostMapping(value = "S2F35_ReportAndEventUnLinked")
    private void S2F35_ReportAndEventUnLinked() throws InterruptedException, SecsException, Secs2Exception {
        List<ReportEventNode> reportEventNodes = new ArrayList<>();

        //把ceid=101 下的所有reportid 全部解绑
        List<ValueNode> rptids = new ArrayList<>();
        reportEventNodes.add(createReportEvent("101",PID10Runnable.CEID_Type,rptids));

        SECSCommand.linkEventReport(reportEventNodes,PID10Runnable.comm);
    }

    @PostMapping(value = "S2F37_enableEventReport")
    private void S2F37_enableEventReport() throws InterruptedException, SecsException, Secs2Exception {
        List<ReportEventNode> reportEventNodes = new ArrayList<>();

        //enable  ceid=101/102/103
        List<String> ceids = new ArrayList<>();
        ceids.add("101");
        ceids.add("102");
        ceids.add("103");

        //ceed = enable event report or disable event report
        SECSCommand.enableEventReport(ceids,PID10Runnable.CEID_Type,true,PID10Runnable.comm);
    }

    @PostMapping(value = "S2F37_enableAllEventReport")
    private void S2F37_enableAllEventReport() throws InterruptedException, SecsException, Secs2Exception {
        List<ReportEventNode> reportEventNodes = new ArrayList<>();

        //enable all ceid
        List<String> ceids = new ArrayList<>();

        //ceed = enable event report or disable event report
        SECSCommand.enableEventReport(ceids,PID10Runnable.CEID_Type,true,PID10Runnable.comm);
    }

    @PostMapping(value = "S2F41")
    private void S2F41() throws InterruptedException, SecsException, Secs2Exception {
        List<CPArgNode> cpArgNodes = new ArrayList<>();
        cpArgNodes.add(new CPArgNode("InfeedState","A","1","U1"));
        cpArgNodes.add(new CPArgNode("ProcInletState","A","21","U1"));
        RCMDNode rcmdNode = new RCMDNode("RRCMD1", "A", cpArgNodes);
        SECSCommand.hostCommandSend(rcmdNode,PID10Runnable.comm);
    }

    @PostMapping(value = "S2F49")
    private void S2F49() throws InterruptedException, SecsException, Secs2Exception {
        List<CPArgNode> cpArgNodes = new ArrayList<>();
        cpArgNodes.add(new CPArgNode("InfeedState","A","1","U1"));
        cpArgNodes.add(new CPArgNode("ProcInletState","A","21","U1"));
        ERCMDNode rcmdNode = new ERCMDNode( "1","RRCMD1","A", cpArgNodes);
        SECSCommand.enhancedRemoteCommand(rcmdNode,PID10Runnable.comm);
    }

    @PostMapping(value = "S5F3")
    private void S5F3() throws InterruptedException, SecsException, Secs2Exception {
//        //1.数组 list,enable/disable 具体的几个alid
//        int[] alids = new int[3];
//        alids[0] = 1;
//        alids[1] = 2;
//        alids[2] = 3;
//        SECSCommand.alarmEDManager(alids,"U4",true,PID10Runnable.comm);


        //2.enable/disable 所有alid
        int[] alids = new int[0];
        SECSCommand.alarmEDManager(alids,"U4",true,PID10Runnable.comm);
    }

    @PostMapping(value = "S5F5")
    private void S5F5() throws InterruptedException, SecsException, Secs2Exception {
//        //1.数组 list,查询 具体的几个alid
        int[] alids = new int[3];
        alids[0] = 1;
        alids[1] = 2;
        alids[2] = 3;
        SECSCommand.alarmList(alids,"U4",PID10Runnable.comm);


        //2.查询 所有alid
//        int[] alids = new int[0];
//        SECSCommand.alarmList(alids,"U4",PID10Runnable.comm);
    }

    @PostMapping(value = "S6F23")
    private void S6F23() throws InterruptedException, SecsException, Secs2Exception {
        //Transmit Spooled Messages
        SECSCommand.requestSpooledData(0,PID10Runnable.comm);


        //Purge Spooled Messages
        SECSCommand.requestSpooledData(1,PID10Runnable.comm);
    }

    @PostMapping(value = "S7F19")
    private void S7F19() throws InterruptedException, SecsException, Secs2Exception {
        //Transmit Spooled Messages
        SECSCommand.currentEPPDRequest(PID10Runnable.comm);
    }

    public static void main(String[] args) {

        String hex = HexUtil.encodeHexStr("22", CharsetUtil.CHARSET_UTF_8);
        byte[] bytes = HexUtil.decodeHex("22");
        System.out.println(hex);
        System.out.println(Arrays.asList(bytes));
    }

    @PostMapping(value = "S7F5")
    private void S7F5() throws InterruptedException, SecsException, Secs2Exception {
        //Transmit Spooled Messages
        SECSCommand.processProgramRequest("asd","A",PID10Runnable.comm);

        //十进制 22  =》 十六进制 0x16（这里的Byte 用十六进制表示）, 所以配方名 要是byte 16进制的，要转成10进制 作为value 输入以下函数
        //33(十进制)  => 0x21(十六进制)
        SECSCommand.processProgramRequest("33","Binary",PID10Runnable.comm);
    }








}
